#include"Stack.h"

void STInit(ST* st)
{
	assert(st);
	st->a = NULL;
	st->capacity = 0;
	st->top = 0;
}
void STDestroy(ST* st)
{
	assert(st);
	free(st->a);
	st->a = NULL;
}
void STPush(ST* st, STDatatype x)
{
	assert(st);
	if (st->capacity == st->top)
	{
		int newcapacity = st->capacity == 0 ? 4 : st->capacity * 2;
		STDatatype* tmp = (STDatatype*)realloc(st->a, sizeof(STDatatype) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc failed");
			return -1;
		}
		st->a = tmp;
		st->capacity = newcapacity;
	}
	st->a[st->top] = x;
	st->top++;
}
void STPop(ST* st)
{
	assert(st);
	st->top--;
}

int STSize(ST* st)
{
	assert(st);
	return st->top;
}
int STTop(ST* st)
{
	assert(st);
	return st->a[st->top - 1];
};
bool STEmpty(ST* st)
{
	assert(st);
	return st->top == 0;
}